﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OneSystems.Data.Entities;
using OneSystems.BusinessLogic;

namespace UserInterface
{
    public partial class MainForm : Form
    {
        private int m_CurrentCategory = 1;
        private int m_CurrentAccount;
        private Debtor m_CurrentDebtor;

        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeUser();
            InitializeDebtHolder();

        }
        #region Private Methods
        private void InitializeUser()
        {
            m_CurrentDebtor = DebtorLogic.GetUser();
        }
        private void InitializeDebtHolder()
        {
            m_DebtHolder.Groups.Clear();
            foreach (OneSystems.Data.Entities.Category category in OneSystems.BusinessLogic.DebtLogic.Categories)
            {
                Infragistics.Win.UltraWinListBar.Group group = new Infragistics.Win.UltraWinListBar.Group();
                group.Text = category.Name;
                group.Key = category.CategoryID.ToString();
                foreach (Account account in DebtLogic.GetAccountsByCategory(category.CategoryID, m_CurrentDebtor.DebtorID))
                {
                    Infragistics.Win.UltraWinListBar.Item item = new Infragistics.Win.UltraWinListBar.Item();
                    item.Appearance.Image = account.Logo;
                    item.Text = string.Format("{0}\n{1}", account.AccountNickname, account.CurrentBalance);
                    item.Key = account.AccountID.ToString();
                    group.Items.Add(item);
                }

                m_DebtHolder.Groups.Add(group);
            }
        }
        private void SendStatusBarMessage(string message)
        {
            this.Cursor = Cursors.WaitCursor;
            StatusBar.Text = message;
            this.Cursor = Cursors.Default;
        }
        private void SendStatusBarMessage()
        {
            SendStatusBarMessage("Ready!");
        }
        private void LoadCalendar()
        {
            DataTable dt = TransactionLogic.GetNormalPaymentChart(m_CurrentAccount, (int)PledgeAmount.Value);
            foreach (DataRow row in dt.Rows)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(string.Format("Payment Due:\n"));
                sb.Append(string.Format("Creditor: {0}\n", row["Description"]));
                sb.Append(string.Format("Amount: {0}\n", row["Payment"]));
                sb.Append(string.Format("Balance: {0}\n", row["Balance"]));
                Infragistics.Win.UltraWinSchedule.Note note = new Infragistics.Win.UltraWinSchedule.Note(DateTime.Parse(row["Date"].ToString()));

                note.Description = sb.ToString();
                note.Appearance.BackColor = System.Drawing.Color.AliceBlue;
                DebtScheduleInfo.Notes.Add(note);
            }
            

        }
        #endregion

        #region Event Handlers
        private void ultraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e)
        {

        }

        private void toolbar_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "File":    // PopupMenuTool
                    // Place code here
                    break;

                case "Insert":    // PopupMenuTool
                    // Place code here
                    break;

                case "View":    // PopupMenuTool
                    // Place code here
                    break;

                case "Options":    // PopupMenuTool
                    // Place code here
                    break;

                case "Help":    // PopupMenuTool
                    // Place code here
                    break;

                case "Connect":    // ButtonTool
                    // Place code here
                    break;

                case "Disconnect":    // ButtonTool
                    // Place code here
                    break;

                case "Print Setup":    // ButtonTool
                    // Place code here
                    break;

                case "Print":    // ButtonTool
                    // Place code here
                    break;

                case "New Brand":    // ButtonTool
                    HelperForms.EditBrand editBrand = new UserInterface.HelperForms.EditBrand();
                    editBrand.BrandID = 0;
                    editBrand.ShowDialog(this);
                    break;

                case "New Category":    // ButtonTool
                    // Place code here
                    break;

                case "New Creditor":    // ButtonTool
                    // Place code here
                    break;
                case "Edit Categoory":    // ButtonTool
                    HelperForms.EditCategory editCategory = new UserInterface.HelperForms.EditCategory();
                    editCategory.Category = m_CurrentCategory;
                    editCategory.ShowDialog(this);
                    break;
                case "Exit":    // ButtonTool
                    this.Close();
                    break;
                case "New Account":    // ButtonTool
                    HelperForms.EditAccount newAccount = new UserInterface.HelperForms.EditAccount();
                    newAccount.AccountID = 0;
                    newAccount.CategoryID = m_CurrentCategory;
                    newAccount.DebtorID = m_CurrentDebtor.DebtorID;
                    newAccount.ShowDialog(this);
                    InitializeDebtHolder();
                    break;

                case "Edit Account":    // ButtonTool
                    HelperForms.EditAccount editAccount = new UserInterface.HelperForms.EditAccount();
                    editAccount.AccountID = m_CurrentAccount;
                    editAccount.CategoryID = m_CurrentCategory;
                    editAccount.DebtorID = m_CurrentDebtor.DebtorID;
                    editAccount.ShowDialog(this);
                    InitializeDebtHolder();
                    break;
                case "Change Pledge Amount":    // ButtonTool
                    HelperForms.EditDebtor editDebtor = new UserInterface.HelperForms.EditDebtor();
                    editDebtor.DebtorID = m_CurrentDebtor.DebtorID;
                    editDebtor.ShowDialog(this);
                    break;

            }

        }

        private void m_DebtHolder_GroupSelected(object sender, Infragistics.Win.UltraWinListBar.GroupEventArgs e)
        {
            if (e.Group != null)
            {
                if (e.Group.Key.Length > 0)
                {
                    m_CurrentCategory = int.Parse(e.Group.Key);
                }
            }
        }

        private void m_DebtHolder_ItemSelected(object sender, Infragistics.Win.UltraWinListBar.ItemEventArgs e)
        {
            if (e.Item.Key.Length > 0)
            {
                SendStatusBarMessage("Loading Account Data....");
                Infragistics.Win.UltraWinToolbars.ButtonTool tool = (Infragistics.Win.UltraWinToolbars.ButtonTool)toolbar.Tools["Edit Account"];
                tool.SharedProps.Enabled = true;

                m_CurrentAccount = int.Parse(e.Item.Key);
                TransactionGrid.DataSource = TransactionLogic.GetTransactionHistory(m_CurrentAccount);
                TransactionGrid.DataBind();

                //PaymentGrid.DataSource = TransactionLogic.GetNormalPaymentChart(m_CurrentAccount, (int)PledgeAmount.Value);
                //PaymentGrid.DataBind();
                PaymentGrid.DataSource = TransactionLogic.GetFuturePayments(m_CurrentAccount);
                DebtReductionChart.DataSource = TransactionLogic.GetNormalChart(m_CurrentAccount, (int)PledgeAmount.Value);
                DebtReductionChart.DataMember = "Balance";
                DebtReductionChart.DataBind();
                LoadCalendar();
                SendStatusBarMessage();
            }
        }
        #endregion

        
    }
}
